/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.listview;

import java.io.Serializable;

import android.content.Context;
import android.database.DataSetObserver;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.util.ContactUtils;
import com.phonebooksharing.android.util.GeneralUtils;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.widgets.pagination.PaginatorListView;
import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;

@SuppressWarnings("unchecked")
public class ContactNearbyListView extends PaginatorListView {

    private LayoutInflater inflater;
    public ContactNearbyListView(Context dv, int qty, TextView counter) {
        super(dv, qty, counter);
        inflater = LayoutInflater.from(getContext());
    }
    
    @Override
    public View getConvertView(View cv, Object dto, int position, ViewGroup parent) {
       ViewHolder holder;
       if (cv == null) {
           cv = inflater.inflate(R.layout.list_item_3lines, null);

           holder = new ViewHolder();
           holder.icon = (ImageView) cv.findViewById(android.R.id.icon);
           holder.border = (View) cv.findViewById(android.R.id.background);
           
           holder.name = (TextView) cv.findViewById(android.R.id.text1);
           holder.time = (TextView) cv.findViewById(R.id.text2);
           holder.status = (TextView) cv.findViewById(R.id.text3);
           holder.location = (TextView) cv.findViewById(R.id.text4);
           
           cv.setTag(holder);
       } else {
           holder = (ViewHolder) cv.getTag();
       }
       
       final DashboardReaderItem d = (DashboardReaderItem)dto;
       final ContactItem cache = (ContactItem)d.getItem();
       
       ContactUtils.setContactPhoto(this, cache, holder.icon, holder.border, position);
       holder.name.setText(cache.name);
       holder.time.setText("(" + DateUtils.formatElapsedTime(
               (GeneralUtils.calculateGmtTimeInMilis(System.currentTimeMillis()) - 
                GeneralUtils.calculateGmtTimeInMilis(d.getWhen()) )/1000) + ")");
       
       holder.status.setText(cache.status);
       holder.location.setText(cache.location);
       
       return cv;
    }
    
    static class ViewHolder {
        ImageView icon;
        TextView name, time, status, location;
        View border;
        int id;
    }

    private TextView counter;
    class CounterUpdater extends DataSetObserver {
        @Override
        public void onChanged() {
            super.onChanged();
            long r = adapter.totalResults, l = adapter.getCount();
            if(l > r) {
                l = r;
            }
            counter.setText("(" + l + " of " + r + ")");
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
            long r = adapter.totalResults, l = adapter.getCount();
            if(l > r) {
                l = r;
            }
            counter.setText("(" + l + " of " + r + ")");
        }
    }

    @Override
    public String getLoadDataURL() {
        return String.format(Global.DASHBOARD_SERVLET_URL, "gc" /*ac*/, "" /*q*/, currentPage++ /*p*/, Global.PAGINATOR_PAGE_SIZE /*ps*/);
    }

    @Override
    public Serializable getObject2Send() {
        return Global.authItem;
    }
}
